﻿/******************************************************************************
* 【本类功能概述】                                 					      *
*  版权所有（C）2020-20XX，                                        *
*  保留所有权利。                                                            *
******************************************************************************
*  作者 : <hughjk>
*  创建时间:2019/11/24 23:40:03
*  文件描述:建造
*****************************************************************************/

using UnityEngine;

namespace StateModel
{
    public class ScvBuildState : State
    {
        public Barracks[] barracksAll;

        public Barracks Find(int id)
        {
            for (int i = 0; i < barracksAll.Length; i++) {
                if (id == barracksAll[i]._Id) {
                    return barracksAll[i];
                }
            }
            return null;
        }

        public Barracks barracks;
        public BarrackInBuildState InBuildState;

        public void Build(int id, Vector3 pos)
        {
            barracks = Find(id);
            barracks = GameObject.Instantiate(barracks, pos, Quaternion.identity);
            barracks.gameObject.SetActive(true);
            barracks._TeamType = m_controller._TeamType;
            barracks.isInit = false;
            barracks.Init();
            barracks.m_StateManage.SwitchState(StateType.Build);

            Build(barracks);
        }

        public void Build(Barracks _barracks)
        {
            barracks = _barracks;
            InBuildState = barracks.m_StateManage.Fin<BarrackInBuildState>(StateType.Build);
            SwitchState(StateType.Build);
        }

        public override void OnUpdata(float dt)
        {
            base.OnUpdata(dt);
            if (InBuildState) {
                InBuildState.CurProgress += Time.deltaTime;
                if (InBuildState.CurProgress >= InBuildState.m_controller._DrillTime) {
                    InBuildState.SwitchState(StateType.Idle);
                    SwitchState(StateType.Idle);
                }
            }
        }
    }
}